//
// Copyright 2021 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// stylelint-disable selector-class-pattern --
// Selector '.mdc-*' should only be used in this project.

@use '@material/dom/dom';
@use '@material/feature-targeting/feature-targeting';
@use '@material/focus-ring/focus-ring';
@use '@material/ripple/ripple';
@use '@material/ripple/ripple-theme';
@use '@material/touch-target/mixins' as touch-target-mixins;
@use './icon-button-theme';

@mixin core-styles($query: feature-targeting.all()) {
  @include without-ripple($query);
  @include ripple($query);
}

@mixin static-styles($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  // postcss-bem-linter: define icon-button
  .mdc-icon-button {
    @include feature-targeting.targets($feat-structure) {
      display: inline-block;
      position: relative;
      box-sizing: border-box;
      border: none;
      outline: none;
      background-color: transparent;
      fill: currentColor;
      color: inherit;
      text-decoration: none;
      cursor: pointer;
      user-select: none;
      z-index: 0; // Added to render above the container in IE11 tests.
      overflow: visible; // Added to fix IE11 touch target tests.
    }

    .mdc-icon-button__touch {
      @include touch-target-mixins.touch-target(
        $set-width: true,
        $query: $query
      );
    }

    @include ripple-theme.focus {
      .mdc-icon-button__focus-ring {
        @include dom.forced-colors-mode($exclude-ie11: true) {
          @include feature-targeting.targets($feat-structure) {
            display: block;
          }
        }
      }
    }

    @include if-disabled_ {
      @include feature-targeting.targets($feat-structure) {
        cursor: default;
        pointer-events: none;
      }
    }

    &[hidden] {
      @include feature-targeting.targets($feat-structure) {
        display: none;
      }
    }
  }

  .mdc-icon-button--display-flex {
    @include feature-targeting.targets($feat-structure) {
      align-items: center;
      display: inline-flex;
      justify-content: center;
    }
  }

  .mdc-icon-button__focus-ring {
    @include focus-ring.focus-ring(
      $query: $query,
      $container-outer-padding-vertical: 0,
      $container-outer-padding-horizontal: 0
    );

    @include feature-targeting.targets($feat-structure) {
      display: none;
    }
  }

  .mdc-icon-button__icon {
    @include feature-targeting.targets($feat-structure) {
      display: inline-block;
    }

    &.mdc-icon-button__icon--on {
      @include feature-targeting.targets($feat-structure) {
        display: none;
      }
    }
  }

  .mdc-icon-button--on {
    .mdc-icon-button__icon {
      @include feature-targeting.targets($feat-structure) {
        display: none;
      }

      &.mdc-icon-button__icon--on {
        @include feature-targeting.targets($feat-structure) {
          display: inline-block;
        }
      }
    }
  }
  // postcss-bem-linter: end

  .mdc-icon-button__link {
    @include feature-targeting.targets($feat-structure) {
      height: 100%;
      left: 0;
      outline: none;
      position: absolute;
      top: 0;
      width: 100%;
    }
  }
}

@mixin without-ripple($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  .mdc-icon-button {
    @include feature-targeting.targets($feat-structure) {
      font-size: icon-button-theme.$icon-size;
    }

    @include icon-button-theme.density(0, $query: $query);

    @include icon-button-theme.disabled-ink-color(
      text-disabled-on-light,
      $query: $query
    );

    svg,
    img {
      @include feature-targeting.targets($feat-structure) {
        width: icon-button-theme.$icon-size;
        height: icon-button-theme.$icon-size;
      }
    }
  }

  @include static-styles($query: $query);
}

@mixin ripple($query: feature-targeting.all()) {
  @include ripple.common($query); // COPYBARA_COMMENT_THIS_LINE

  .mdc-icon-button {
    @include ripple.surface(
      $query: $query,
      $ripple-target: icon-button-theme.$ripple-target
    );
    @include ripple.radius-unbounded(
      $query: $query,
      $ripple-target: icon-button-theme.$ripple-target
    );
    @include ripple-theme.states(
      $query: $query,
      $ripple-target: icon-button-theme.$ripple-target
    );

    &:disabled {
      @include ripple-theme.states-opacities(
        (
          hover: 0,
          focus: 0,
          press: 0,
        ),
        $ripple-target: icon-button-theme.$ripple-target,
        $query: $query
      );
    }

    .mdc-icon-button__ripple {
      $feat-structure: feature-targeting.create-target($query, structure);
      @include feature-targeting.targets($feat-structure) {
        height: 100%;
        left: 0px;
        pointer-events: none;
        position: absolute;
        top: 0px;
        width: 100%;
        z-index: -1;
      }
    }
  }
}

///
/// Helps style the icon button in its disabled state.
/// @access private
///
@mixin if-disabled_ {
  &:disabled {
    @content;
  }
}
